/*
 * RenameDialog.java
 *
 * Created on 2 February, 2009, 4:49 PM
 */

package net.sf.bluex.explorer.boundary;

import java.io.File;
import net.sf.bluex.boundary.BaseWindow;
import net.sf.bluex.components.BlueXStatics;
import net.sf.bluex.controller.BaseWindowManagement;
import net.sf.bluex.controller.UsefulMethods;

/**
 *
 * @author  Blue
 */
public class NewFolderDialog extends javax.swing.JDialog {

    private java.io.File parentFile;
    private BaseWindow bw;
    
    /** Creates new form RenameDialog */
    public NewFolderDialog(BaseWindow parent, File file) {
        super(parent, true);
        this.bw=parent;
        initComponents();
        this.parentFile=file;
        initFurther();
        doKeyReleasedJob();
        this.setIconImage(net.sf.bluex.controller.UsefulMethods.getImage(net.sf.bluex.controller.FileModule.APP_ICON));
        this.setVisible(true);
    }
    
    /**
     * private methods
     */
    private void initFurther(){
        //frame init
        this.setLocationRelativeTo(bw);
        this.setTitle("Create:");
        this.getRootPane().setDefaultButton(cmdCreate);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent we){
                doCancelJob();
            }
        });
        
        //set the parent text field
        txtParent.setText(parentFile.getAbsolutePath());
        txtNewFileName.requestFocus();
        txtNewFileName.setText(getSuggestedName());
        txtNewFileName.selectAll();
    }

    private String getSuggestedName(){
        String newFolder="New Folder";
        File file=new File(parentFile, newFolder);
        int i=1;
        while(file.exists()){
            String folder=newFolder+"("+(i++)+")";
            file=new File(parentFile, folder);
        }
        return file.getName();
    }
    
    private void doCreateJob(){
        boolean success;
        
        java.io.File file=new java.io.File(this.parentFile,txtNewFileName.getText());
        if(jrbFolder.isSelected())
            success=file.mkdir();
        else{
            try{
                success=file.createNewFile();
            }catch(java.io.IOException ioe){
                success=false;
                javax.swing.JOptionPane.showMessageDialog(this, "There is some exception occurred:\n"+ioe);
            }
        }
        
        if(success){
            bw.dataUpdated(new File[]{file}, BaseWindowManagement.NEW_ADDITION);
            doCancelJob();
        }
    }
    
    private void doCancelJob(){
        this.dispose();
    }
    
    private void doKeyReleasedJob(){
        if(txtNewFileName.getText().equals("")){
            lbSummary.setText("Enter valid name.");
            cmdCreate.setEnabled(false);
        }
        else{
            java.io.File tempFile=new java.io.File(parentFile, txtNewFileName.getText());
            boolean found=false;
            
            if(jrbFolder.isSelected())
                found=UsefulMethods.fileExists(parentFile, tempFile, UsefulMethods.FOLDER_MODE);
            else
                found=UsefulMethods.fileExists(parentFile, tempFile, UsefulMethods.FILE_MODE);
            
            if(found){
                lbSummary.setText("This File/Folder name is already available.");
                cmdCreate.setEnabled(false);
            }
            else{
                lbSummary.setText("");
                cmdCreate.setEnabled(true);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        txtNewFileName = new javax.swing.JTextField();
        cmdCancel = new javax.swing.JButton();
        cmdCreate = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jrbFolder = new javax.swing.JRadioButton();
        jrbFile = new javax.swing.JRadioButton();
        lbSummary = new javax.swing.JLabel();
        txtParent = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Create Folder/File");
        setResizable(false);

        jLabel1.setText("Name:");

        txtNewFileName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNewFileNameKeyReleased(evt);
            }
        });

        cmdCancel.setText("Cancel");
        cmdCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelActionPerformed(evt);
            }
        });

        cmdCreate.setText("Create");
        cmdCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCreateActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Type:"));

        buttonGroup1.add(jrbFolder);
        jrbFolder.setSelected(true);
        jrbFolder.setText("New Folder");
        jrbFolder.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jrbFolderStateChanged(evt);
            }
        });

        buttonGroup1.add(jrbFile);
        jrbFile.setText("New File");
        jrbFile.setEnabled(false);
        jrbFile.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jrbFileStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jrbFolder)
                    .addComponent(jrbFile))
                .addContainerGap(227, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jrbFolder)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jrbFile))
        );

        lbSummary.setForeground(new java.awt.Color(255, 0, 0));

        txtParent.setEditable(false);

        jLabel2.setText("Location:");

        jLabel3.setText("Summary:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cmdCreate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdCancel))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNewFileName, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                            .addComponent(txtParent, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                        .addComponent(lbSummary, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cmdCancel, cmdCreate});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtParent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNewFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lbSummary, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdCancel)
                    .addComponent(cmdCreate))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cmdCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCreateActionPerformed
// TODO add your handling code here:
    doCreateJob();
}//GEN-LAST:event_cmdCreateActionPerformed

private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed
// TODO add your handling code here:
    doCancelJob();
}//GEN-LAST:event_cmdCancelActionPerformed

private void txtNewFileNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNewFileNameKeyReleased
// TODO add your handling code here:
    doKeyReleasedJob();
}//GEN-LAST:event_txtNewFileNameKeyReleased

private void jrbFolderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jrbFolderStateChanged
// TODO add your handling code here:
    doKeyReleasedJob();
}//GEN-LAST:event_jrbFolderStateChanged

private void jrbFileStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jrbFileStateChanged
// TODO add your handling code here:
    doKeyReleasedJob();
}//GEN-LAST:event_jrbFileStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cmdCancel;
    private javax.swing.JButton cmdCreate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jrbFile;
    private javax.swing.JRadioButton jrbFolder;
    private javax.swing.JLabel lbSummary;
    private javax.swing.JTextField txtNewFileName;
    private javax.swing.JTextField txtParent;
    // End of variables declaration//GEN-END:variables

}
